#include <iostream>
#include <pthread.h>
#include <unistd.h>

int abc;
pthread_mutex_t lock;
pthread_cond_t cond;
void* print(void* agd)
{
  char* arr=(char*)agd;
  int a=1;
  while(true)
  {
    sleep(1);
    pthread_mutex_lock(&lock);
    if(!(a%5))
    {
      pthread_cond_wait(&cond,&lock);
      std::cout<<"唤醒"<<pthread_self()<<std::endl;
    }
    a=(++abc);
    std:: cout<<arr<<' '<<a<<std::endl;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&lock);
  }
}
int main()
{
  pthread_mutex_init(&lock,nullptr);
  pthread_cond_init(&cond,nullptr);
  pthread_t se1,se2,se3,se4;

  pthread_create(&se1,nullptr,print,(void*)"nihao 1");
  pthread_create(&se2,nullptr,print,(void*)"nihao 2");
  pthread_create(&se3,nullptr,print,(void*)"nihao 3");
  pthread_create(&se4,nullptr,print,(void*)"nihao 4");


  pthread_join(se1,nullptr);
  pthread_join(se2,nullptr);
  pthread_join(se3,nullptr);
  pthread_join(se4,nullptr);
  pthread_join(se4,nullptr);

  pthread_mutex_destroy(&lock);
  pthread_cond_destroy(&cond);
  return 0;
}

